const jsonfile = require('jsonfile');
module.exports = {
    '/':(req, res)=>{
        const platformShop=jsonfile.readFileSync(`data/shop.json`)
        res.send({
            code:0,
            data:platformShop,
            msg:'请求成功'
        });
    },
    '/set':(req, res)=>{
        const {platform,code}=req.body
        if(!platform || !code) return res.send({code:1,msg:'平台和CODE不能为空'});
        let platformShop={};
        try {
            platformShop=jsonfile.readFileSync(`data/shop.json`)
        }catch {}
        platformShop[code]=platform
        jsonfile.writeFileSync(`data/shop.json`,platform)
        jsonfile.writeFileSync(`data/shop/${platform}.json`, {})
        res.send({
            code: 0,
            msg: '操作成功',
        })
    },
    '/del':(req, res)=>{
        const {platform}=req.body
        if(!platform) return res.send({code:1,msg:'平台不能为空'});
        const platformTags=jsonfile.readFileSync(`data/shop/${platform}.json`)
        const platformShop=jsonfile.readFileSync(`data/shop.json`)
        if(Object.keys(platformTags).length){
            return res.send({
                code: 1,
                msg: '平台下有菜单，禁止删除',
            })
        }else {
            delete platformShop[platform]
            jsonfile.writeFileSync(`data/shop.json`,platform)
            jsonfile.writeFileSync(`data/shop/${platform}.json`, {})
            res.send({
                code: 1,
                msg: '删除成功',
            })
        }
    },
}
